<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>为语句设置默认的获取模式。</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="pdostatement.setattribute.html">« PDOStatement::setAttribute</a></li>
      <li style="float: right;"><a href="class.pdoexception.html">PDOException »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.pdostatement.html">PDOStatement</a></li>
    <li>为语句设置默认的获取模式。</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="pdostatement.setfetchmode" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">PDOStatement::setFetchMode</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8, PECL pdo &gt;= 0.2.0)</p><p class="refpurpose"><span class="refname">PDOStatement::setFetchMode</span> &mdash; <span class="dc-title">
   为语句设置默认的获取模式。
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-pdostatement.setfetchmode-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>PDOStatement::setFetchMode</strong></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$mode</code></span>): <span class="type">bool</span></div>


  <div class="methodsynopsis dc-description"><span class="methodname"><strong>PDOStatement::setFetchMode</strong></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$PDO::FETCH_COLUMN</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$colno</code></span>): <span class="type">bool</span></div>


  <div class="methodsynopsis dc-description"><span class="methodname"><strong>PDOStatement::setFetchMode</strong></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$PDO::FETCH_CLASS</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$classname</code></span>, <span class="methodparam"><span class="type">array</span> <code class="parameter">$ctorargs</code></span>): <span class="type">bool</span></div>


  <div class="methodsynopsis dc-description"><span class="methodname"><strong>PDOStatement::setFetchMode</strong></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$PDO::FETCH_INTO</code></span>, <span class="methodparam"><span class="type">object</span> <code class="parameter">$object</code></span>): <span class="type">bool</span></div>


 </div>

 <div class="refsect1 parameters" id="refsect1-pdostatement.setfetchmode-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">mode</code></dt>

     <dd>

      <p class="para">
       获取模式必须是 <code class="literal">PDO::FETCH_*</code> 系列常量中的一个。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">colno</code></dt>

     <dd>

      <p class="para">
       列号。
      </p>
     </dd>

    
    
    <dt>
<code class="parameter">classname</code></dt>

     <dd>

      <p class="para">
       类名。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">ctorargs</code></dt>

     <dd>

      <p class="para">
       构造函数参数。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">object</code></dt>

     <dd>

      <p class="para">
       对象。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-pdostatement.setfetchmode-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>true</code></strong>， 或者在失败时返回 <strong><code>false</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-pdostatement.setfetchmode-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-1083">
    <p><strong>示例 #1 设置获取模式</strong></p>
    <div class="example-contents"><p>
     The following example demonstrates how
     <span class="function"><strong>PDOStatement::setFetchMode()</strong></span> changes the default fetch
     mode for a PDOStatement object.下面的例子示范如何用 <span class="function"><strong>PDOStatement::setFetchMode()</strong></span>  来为一个 PDOStatement 对象更改默认的获取模式。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$sql&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'SELECT&nbsp;name,&nbsp;colour,&nbsp;calories&nbsp;FROM&nbsp;fruit'</span><span style="color: #007700">;<br />try&nbsp;{<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$stmt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #0000BB">$sql</span><span style="color: #007700">);<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setFetchMode</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_NUM</span><span style="color: #007700">);<br />&nbsp;&nbsp;while&nbsp;(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch</span><span style="color: #007700">())&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\t"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\t"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;}<br />}<br />catch&nbsp;(</span><span style="color: #0000BB">PDOException&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;print&nbsp;</span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">();<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
apple   red     150
banana  yellow  250
orange  orange  300
kiwi    brown   75
lemon   yellow  25
pear    green   150
watermelon      pink    90
</pre></div>
    </div>
   </div>
  </p>
 </div>


 


</div></div></div></body></html>